/**
 * Created by LMN on 2016/7/22.
 */
var global = {
    vehicles : {},
    callbacks : {},
    pushOneDataAcquireCallback: function(key,callback){
        if(global.callbacks[key]) return false;
        else{
            global.callbacks[key] = callback;
            return true;
        }
    },
    all_callbacks : {},
    pushAllStationAcquireCallback: function(key, callback){
        if(global.all_callbacks[key]) return false;
        else{
            global.all_callbacks[key] = callback;
            return true;
        }
    }
};
var socket = io.connect('/');
socket.on('connect',function(){
    $.ajax({
        url: '/users/login',
        method: 'get',
        success: function(login){
            if(login.error) { location.assign('/login.html');return; }
            socket.emit('login',login);
            global.user = login;
            setInterval(function(){ $.ajax({url: '/heartbeat',method: 'get'});},180000);
            $('#user-name-corner').text(login.username);
            switch (login.auth)
            {
                case 0:login.auth = "超级管理员";break;
                default:login.auth = "测试";break;
            }
            $('#user-name-auth').text(login.auth);
            $.ajax({
                url: '/inspector/all',
                method: 'get',
                success: function(msg){
                    for(var all_key in global.all_callbacks)
                        global.all_callbacks[all_key](msg);
                    global.inspector = msg;
                    for(var index in msg){
                        var code = msg[index].code;
                        socket.on(code+'',function(data){
                            global.inspector[data.code] = data;
                            for(var key in global.callbacks){
                                global.callbacks[key](data);
                            }
                        });
                    }
                }
            });
        }
    });
});